/*
 * CalendarService.java
 * 
 * Created by demory on Aug 21, 2009, 9:13:18 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.core.transit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 *
 *
 * @author demory
 */
public class CalendarService {

  private String id_, name_;

  // base weekly service range
  private Set<Integer> days_;
  private Date start_, end_;

  // added/subtracted dates, stored as strings in MMDDYYYY format
  private Set<String> additions_, subtractions_;

  //private Calendar calendar_;
  private SimpleDateFormat dfMMDDYYYY_;

  public CalendarService(String id, String name) {
    id_ = id;
    name_ = name;
    additions_ = new HashSet<String>();
    subtractions_ = new HashSet<String>();

    //calendar_ = new GregorianCalendar();
    dfMMDDYYYY_ = new SimpleDateFormat("MMddyyyy");

  }

  public String getID() {
    return id_;
  }

  public String getName() {
    return name_;
  }

  public void setWeeklySchedule(Set<Integer> days) {
    days_ = new HashSet<Integer>();
    for(int day : days) days_.add(day);
  }

  public void setWeeklySchedule(Set<Integer> days, String start, String end) {
    try {
      setWeeklySchedule(days, dfMMDDYYYY_.parse(start), dfMMDDYYYY_.parse(end));
    } catch (ParseException ex) {
      Logger.getLogger(CalendarService.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  public void setWeeklySchedule(Set<Integer> days, Date start, Date end) {
    days_ = new HashSet<Integer>();
    for(int day : days) days_.add(day);
    start_ = start;
    end_ = end;
  }

  public void newAddition(Date date) {
    newAddition(dfMMDDYYYY_.format(date));
  }

  public void newAddition(String dateStr) {
    additions_.add(dateStr);
  }

  public void newSubstraction(Date date) {
    newSubtraction(dfMMDDYYYY_.format(date));
  }

  public void newSubtraction(String dateStr) {
    subtractions_.add(dateStr);
  }

  public boolean isDateServed(Date date) {

    if(additions_.contains(dfMMDDYYYY_.format(date))) return true;
    if(subtractions_.contains(dfMMDDYYYY_.format(date))) return false;

    if(start_ != null && date.before(start_)) return false;
    if(end_ != null && date.after(end_)) return false;

    //calendar_.setTime(date);

    Calendar cal = new GregorianCalendar();
    cal.setTime(date);
    return days_.contains(cal.get(Calendar.DAY_OF_WEEK));
  }

  public boolean containsDay(int day) {
    return days_.contains(day);
  }

  String getDaysString() {
    String days = "";
    for(int day : days_)
      days += (""+day);
    return days;
  }

}
